package com.mall.controller;

import com.mall.model.vo.ProductAttributeAndCategoryVo;
import com.mall.service.ProductAttributeService;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.domain.PmsProductAttributeCategory;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.productAttributeCategoryWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {
    @Autowired
    private ProductAttributeService service;

    /**
     * 分页获取所有商品属性分类
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/category/list")
    public CommonResult queryAllProductAttributeCategory(Integer pageNum, Integer pageSize) {
        PageResult pr = service.queryAllProductAttributeCategory(pageNum, pageSize);
        return CommonResult.success(pr);
    }

    /**
     * 添加商品属性分类
     *
     * @param name
     * @return
     */
    @PostMapping("/category/create")
    public CommonResult save(String name) {
        service.save(name);
        return CommonResult.success(null, "添加成功");
    }


    /**
     * 删除单个商品属性分类
     *
     * @param id
     * @return
     */
    @GetMapping("/category/delete/{id}")
    public CommonResult deleteById(@PathVariable Integer id) {
        service.deleteById(id);
        return CommonResult.success(null, "删除成功");
    }


    /**
     * 修改商品属性分类
     *
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/category/update/{id}")
    public CommonResult updateById(@PathVariable Integer id, String name) {
        service.updateById(id, name);
        return CommonResult.success(null, "修改成功");
    }


    /**
     * 获取单个商品属性分类
     *
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public CommonResult queryById(@PathVariable Integer id) {
        PmsProductAttributeCategory ppac = service.queryById(id);
        return CommonResult.success(null);
    }


    /**
     * 获取所有商品属性分类及其下属性
     *
     * @return
     */
    @GetMapping("/category/list/withAttr")
    public CommonResult queryAllWithAttr() {
        List<productAttributeCategoryWithAttrVo> vo = service.queryAllWithAttr();
        return CommonResult.success(vo);
    }


    /**
     * 添加商品属性信息
     *
     * @param productAttribute
     * @return
     */
    @PostMapping("/create")
    public CommonResult addProductAttribute(@RequestBody PmsProductAttribute productAttribute) {
        service.addProductAttribute(productAttribute);
        return CommonResult.success(null, "添加成功");
    }


    /**
     * 批量删除商品属性
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult deleteBatch(@RequestParam("ids") List<Integer> ids) {
        service.deleteBatch(ids);
        return CommonResult.success(null, "删除成功");
    }


    /**
     * 修改商品属性信息
     *
     * @param id
     * @param productAttribute
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult updateAttributeById(@PathVariable Integer id,
                                            @RequestBody PmsProductAttribute productAttribute) {
        service.updateAttributeById(id, productAttribute);
        return CommonResult.success(null, "修改成功");
    }


    /**
     * 查询单个商品属性
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult queryAttributeById(@PathVariable Integer id) {
        PmsProductAttribute productAttribute = service.queryAttributeById(id);
        return CommonResult.success(productAttribute);
    }


    /**
     * 根据分类查询属性列表或参数列表
     *
     * @param cid
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{cid}")
    public CommonResult queryAttributeOrParamList(@PathVariable Integer cid, Integer type,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pr = service.queryAttributeOrParamList(cid, type, pageNum, pageSize);
        return CommonResult.success(pr);
    }


    /**
     * 根据商品分类的id获取商品属性及属性分类
     *
     * @param productCategoryId
     * @return
     */
    @GetMapping("/attrInfo/{productCategoryId}")
    public CommonResult queryProductAttributeAndAttributeCategory(@PathVariable Integer productCategoryId) {
        List<ProductAttributeAndCategoryVo> vos = service.queryProductAttributeAndAttributeCategory(productCategoryId);
        return CommonResult.success(vos);
    }


}
